{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6f2d2d00",
   "metadata": {},
   "source": [
    "## Running a Bash Command\n",
    "\n",
    "Run a Bash command that returns information from the OS.\n",
    "\n",
    "### Procedure\n",
    "\n",
    "1. Write a `lepton` that invokes a Bash command. This example returns the current version of the gcc compiler. More advanced (and useful) applications might invoke a Makefile or a build command on a remote development machine."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bf22ed6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import covalent as ct\n",
    "\n",
    "task = ct.Lepton(\n",
    "    language=\"bash\",\n",
    "    command=\"version=`gcc --version | awk 'NR==1 {{print $3}}'` \\\n",
    "        && IFS=. read major minor patch <<< $version \\\n",
    "    \",\n",
    "    argtypes=[\n",
    "        (str, ct.Lepton.INPUT_OUTPUT),\n",
    "        (int, ct.Lepton.OUTPUT),\n",
    "        (int, ct.Lepton.OUTPUT),\n",
    "        (int, ct.Lepton.OUTPUT)\n",
    "    ],\n",
    "    named_outputs=[\"CPP\", \"major\", \"minor\", \"patch\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bfc704f",
   "metadata": {},
   "source": [
    "The `named_outputs` parameter identifies the environment variables from which to read the script output. You must specify the output types in the `argtypes` array when constructing the Lepton. In this example, the gcc major version, minor version, and patch version are saved to the `read`, `minor`, and `patch` env variables respectively, and each is to be interpreted as a Python `int`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa1f26d6",
   "metadata": {},
   "source": [
    "2. Run the task and print the result:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d950c4b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Lattice Result\n",
      "==============\n",
      "status: FAILED\n",
      "result: None\n",
      "input args: []\n",
      "input kwargs: {'CPP': 'gcc'}\n",
      "error: Node version=`gcc --version | awk 'NR==1 {{print $3}}'`         && IFS=. read major minor patch <<< $version      failed: \n",
      "Traceback (most recent call last):\n",
      "  File \"/Users/mini-me/opt/anaconda3/lib/python3.9/site-packages/covalent_dispatcher/_core/execution.py\", line 353, in _run_task\n",
      "    output, stdout, stderr = await execute_callable()\n",
      "  File \"/Users/mini-me/opt/anaconda3/lib/python3.9/site-packages/covalent/executor/base.py\", line 387, in execute\n",
      "    result = await self.run(function, args, kwargs, task_metadata)\n",
      "  File \"/Users/mini-me/opt/anaconda3/lib/python3.9/site-packages/covalent/executor/executor_plugins/dask.py\", line 112, in run\n",
      "    result, worker_stdout, worker_stderr = await dask_client.gather(future)\n",
      "  File \"/Users/mini-me/opt/anaconda3/lib/python3.9/site-packages/distributed/client.py\", line 2037, in _gather\n",
      "    raise exception.with_traceback(traceback)\n",
      "  File \"/Users/mini-me/opt/anaconda3/lib/python3.9/site-packages/covalent/executor/executor_plugins/dask.py\", line 60, in dask_wrapper\n",
      "    output = fn(*args, **kwargs)\n",
      "  File \"/Users/mini-me/opt/anaconda3/lib/python3.9/site-packages/covalent/executor/base.py\", line 92, in wrapper_fn\n",
      "    output = fn(*new_args, **new_kwargs)\n",
      "  File \"/Users/mini-me/opt/anaconda3/lib/python3.9/site-packages/covalent/_workflow/lepton.py\", line 368, in shell_wrapper\n",
      "    getattr(builtins, self.argtypes[idx][0])(\n",
      "ValueError: invalid literal for int() with base 10: 'version'\n",
      "\n",
      "\n",
      "start_time: 2022-12-29 03:19:14.002622\n",
      "end_time: 2022-12-29 03:19:14.168102\n",
      "\n",
      "results_dir: /Users/mini-me/agnostiq/covalent/doc/source/how_to/coding/results\n",
      "dispatch_id: 502074bf-5b01-43db-8a44-30a34620f338\n",
      "\n",
      "Node Outputs\n",
      "------------\n",
      "version=`gcc --version | awk 'NR==1 {{print $3}}'`         && IFS=. read major minor patch <<< $version     (0): None\n",
      ":parameter:gcc(1): gcc\n",
      "\n"
     ]
    }
   ],
   "source": [
    "@ct.lattice\n",
    "def version_workflow(**kwargs) -> str:\n",
    "    return task(**kwargs)\n",
    "\n",
    "result = ct.dispatch_sync(version_workflow)(CPP=\"gcc\")\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e61bd117-ce8f-4877-ac52-7d6b63d598e8",
   "metadata": {},
   "source": [
    "The result is the version parsed from the output of `gcc --version`. "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
